package com.flightcloud.ssm;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.method.support.CompositeUriComponentsContributor;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

// @EnableWebMvc
// 启动对@AspectJ的支持，并强制使用cglib代理
@EnableAspectJAutoProxy /* (proxyTargetClass = true) */
@ComponentScan(basePackages = "com.flightcloud", useDefaultFilters = false, includeFilters = {
		@ComponentScan.Filter(type = FilterType.ANNOTATION, value = { Controller.class, ControllerAdvice.class }) })
@Import(value = { BeetlConfig.class })
// @Import(value = { FreemarkerConfig.class })
public class MvcConfig extends /* WebMvcConfigurerAdapter */WebMvcConfigurationSupport {

	@Bean
	public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	/**
	 * 开启default-servlet-handler=<mvc:default-servlet-handler/>
	 */
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}

	/**
	 * 添加默认视图
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("index");
		registry.addViewController("/404").setViewName("404");
	}

	// @Override
	// protected void configureViewResolvers(ViewResolverRegistry registry) {
	// // TODO Auto-generated method stub
	// super.configureViewResolvers(registry);
	// }

	@Bean
	public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
		MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
		// 避免IE在ajax请求出现文件下载的情况
		List<MediaType> mediaTypes = new ArrayList<>();
		mediaTypes.add(MediaType.parseMediaType("text/html;charset=UTF-8"));
		mappingJackson2HttpMessageConverter.setSupportedMediaTypes(mediaTypes);
		return mappingJackson2HttpMessageConverter;
	}

	/**
	 * 配置消息converters
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(mappingJackson2HttpMessageConverter());
		addDefaultHttpMessageConverters(converters);
	}

	/**
	 * 配置异常处理器
	 */
	@Override
	public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
		// ExceptionHandlerExceptionResolver exceptionResolver = new
		// ExceptionHandlerExceptionResolver();
		// exceptionResolver.setMessageConverters(getMessageConverters());
		// exceptionResolver.setMappedHandlerClasses(new
		// Class[]{RuntimeException.class});
		// exceptionResolvers.add(exceptionResolver);
		super.configureHandlerExceptionResolvers(exceptionResolvers);
	}

	// @Override
	// protected void configureContentNegotiation(ContentNegotiationConfigurer
	// configurer) {
	// configurer.mediaType("html", MediaType.TEXT_HTML);
	// configurer.mediaType("xml", MediaType.APPLICATION_XML);
	// configurer.mediaType("json", MediaType.APPLICATION_JSON);
	// super.configureContentNegotiation(configurer);
	// }

	/**
	 * 配置视图解析器
	 */
	// @Override
	// public void configureViewResolvers(ViewResolverRegistry registry) {
	// MappingJackson2JsonView mappingJackson2JsonView = new
	// MappingJackson2JsonView();
	// Set<String> jsonpParameterNames = new HashSet<>();
	// jsonpParameterNames.add("jsonp");
	// jsonpParameterNames.add("callback");
	//
	// mappingJackson2JsonView.setJsonpParameterNames(jsonpParameterNames);
	//
	// registry.enableContentNegotiation(mappingJackson2JsonView);
	// registry.freeMarker().suffix(".html").cache(false);
	// }

	/**
	 * 静态资源配置
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/js/**").addResourceLocations("/WEB-INF/js/");
		super.addResourceHandlers(registry);
	}

	@Bean
	public CompositeUriComponentsContributor mvcUriComponentsContributor(
			RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
		return new CompositeUriComponentsContributor(requestMappingHandlerAdapter.getArgumentResolvers(),
				mvcConversionService());
	}

	@Override
	public FormattingConversionService mvcConversionService() {
		// TODO Auto-generated method stub
		return super.mvcConversionService();
	}

}
